import { Message } from "element-ui";
export function tipErr(msg) {
  Message.close();
  Message.error({
    message: msg,
    offset: 300,
    duration: 2000,
  });
}
export function tipSuc(msg) {
  Message.close();
  Message.success({
    message: msg,
    offset: 300,
    duration: 2000,
  });
}
export function tipWarn(msg) {
  Message.close();
  Message.warning({
    message: msg,
    offset: 300,
    duration: 2000,
  });
}


export function funcChangeNumToCHN(str) {
  let _arrayCHNNum = [
    "〇",
    "一",
    "二",
    "三",
    "四",
    "五",
    "六",
    "七",
    "八",
    "九",
  ];
  let _arrayCHNBit = ["", "十", "百", "千"];
  let _numArray = str.toString().split("");
  if (_numArray.length === 2 && _numArray[0] === "1") {
    _numArray[0] = "y";
  }
  for (let i = _numArray.length - 1; i >= 0; i--) {
    if (_numArray[i] === "0") {
      _numArray[i] = "x";
    } else {
      break;
    }
  }
  let _s = "";
  let _bLast0 = false;
  for (let i = 0; i < _numArray.length; i++) {
    if (_numArray[i] === "x") {
      continue;
    }
    if (_numArray[i] === "0" && _bLast0) {
      _bLast0 = true;
      continue;
    }
    if (_numArray[i] !== "y") {
      //delete 1 in ten
      _s += _arrayCHNNum[parseInt(_numArray[i])];
    }
    if (_numArray[i] === "0") {
      _bLast0 = true;
      continue;
    }
    _bLast0 = false;

    _s += _arrayCHNBit[_numArray.length - i - 1];
  }
  return _s;
}

/*
 * 设置setLocalStorage
 * */
export function setLocalStorage(key, value) {
  window.localStorage.setItem(key, window.JSON.stringify(value));
}
/*
 * 获取getLocalStorage
 * */
export function getLocalStorage(key) {
  return window.JSON.parse(window.localStorage.getItem(key) || "[]");
}
/*
 * 设置setSessionStorage
 * */
export function setSessionStorage(key, value) {
  window.sessionStorage.setItem(key, window.JSON.stringify(value));
}

/*
 * 获取getSessionStorage
 * */
export function getSessionStorage(key) {
  return window.JSON.parse(window.sessionStorage.getItem(key) || "[]");
}
//设置cookie
export function setCookie(cname, cvalue, exdays) {
  var d = new Date();
  d.setTime(d.getTime() + (exdays*24*60*60*1000));
  var expires = "expires="+d.toUTCString();
  document.cookie = cname + "=" + cvalue + "; " + expires;
}
//获取cookie
export function getCookie(cname) {
  var name = cname + "=";
  var ca = document.cookie.split(';');
  for(var i=0; i<ca.length; i++) {
      var c = ca[i];
      while (c.charAt(0)==' ') c = c.substring(1);
      if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
  }
  return "";
}
//清除cookie  
export function clearCookie(name) {  
  setCookie(name, "", -1);  
}  


/*
 * 格式化时间
 * yyyy-MM-dd hh:mm:ss
 * */
export function formatDate(date, fmt) {
  if (!date || date == null) return null;
  let format = fmt || "yyyy-MM-dd hh:mm:ss";

  let dates = new Date(date);

  let formatObj = {
    y: dates.getFullYear(),
    M: dates.getMonth() + 1,
    d: dates.getDate(),
    h: dates.getHours(),
    m: dates.getMinutes(),
    s: dates.getSeconds(),
  };
  let time_str = format.replace(/(y|M|d|h|m|s)+/g, (result, key) => {
    let value = formatObj[key];
    if (result.length > 0 && value < 10) {
      value = "0" + value;
    }
    return value || 0;
  });
  return time_str;
}
